package com.cp.weixin.interceptor;

import com.cp.weixin.controller.IndexController;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        System.err.println("pre handle...");
        HttpSession httpSession = request.getSession();
        Object sessionObj = httpSession.getAttribute("blog-session");
        if(sessionObj != null && IndexController.loginPara.equals(sessionObj.toString())) {
            System.err.println(sessionObj.toString());
            return true;
        } else {
            response.sendRedirect("/toLogin");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        System.err.println("post handle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        System.err.println("after completion");
    }
}
